package com.numberone.master.common.utils;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.numberone.master.common.exception.RespResult;
import com.numberone.master.common.menu.RespCode;

public class ResponseDo {
	/**
	 * 返回json格式数据
	 * @param response
	 * @param errorCode 错误码
	 * @param errMsg 错误提示
	 */
    public static void dealWithAjaxReturn(ServletResponse response, RespCode respCode) {
    	RespResult responseDo = RespResult.getInstance(respCode);
        String responseStr = JSONObject.toJSONString(responseDo);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.append(responseStr);
        } catch (IOException e) {
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
    
    /**
     * 自定错误
     * @param response
     * @param code
     * @param msg
     */
    public static void dealWithAjaxCustomReturn(ServletResponse response, String code,String msg) {
    	RespResult responseDo = RespResult.getInstance(code,msg);
        String responseStr = JSONObject.toJSONString(responseDo);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            out.append(responseStr);
        } catch (IOException e) {
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
}
